function customerChoice(btn) {

    const choice = document.querySelectorAll(".selectForm");
    choice.forEach(l => l.classList.remove("cursel"));
    btn.classList.add("cursel");
    document.getElementById("firsttable").style.display = "";
    document.getElementById("secondtable").style.display = "none";
    document.getElementById("thirdtable").style.display = "none";


}


function bankNumChoice(btn) {
    const choice = document.querySelectorAll(".selectForm");
    choice.forEach(l => l.classList.remove("cursel"));
    btn.classList.add("cursel");

    document.getElementById("firsttable").style.display = "none";
    document.getElementById("secondtable").style.display = "";
    document.getElementById("thirdtable").style.display = "none";

}

function competitorChoice(btn) {
    const choice = document.querySelectorAll(".selectForm");
    choice.forEach(l => l.classList.remove("cursel"));
    btn.classList.add("cursel");

    document.getElementById("firsttable").style.display = "none";
    document.getElementById("secondtable").style.display = "none";
    document.getElementById("thirdtable").style.display = "";
}

function submitForm() {

    if (isEmpty(document.getElementById("custname").value)) {
        alert("客户姓名不能为空");
        return;
    }
    if (isEmpty(document.getElementById("unit").value)) {
        alert("单位性质不能为空");
        return;
    }
    if (isEmpty(document.getElementById("classify").value)) {
        alert("行业分类为空");
        return;
    }
    if (isEmpty(document.getElementById("source").value)) {
        alert("客户来源不能为空");
        return;
    }
    if (isEmpty(document.getElementById("cus_address").value)) {
        alert("联系地址不能为空");
        return;
    }
    if (isEmpty(document.getElementById("serve").value)) {
        alert("是否服务不能为空");
        return;
    }
    if (isEmpty(document.getElementById("address").value)) {
        alert("客户地址不能为空");
        return;
    }
    if (isEmpty(document.getElementById("level").value)) {
        alert("客户等级不能为空");
        return;
    }

    let tb = document.getElementById("firsttable");
    let tbRowlength = tb.rows.length
    while (tbRowlength > 1) {
        let length = document.getElementsByClassName("second_name").length;
        let i = 0;
        for (; length > 0; length--,i++) {
            console.log("length:" + length);
            console.log("i:" + i);
            console.log("1111111:" + document.getElementsByClassName("second_name")[i].value);
            if (isEmpty(document.getElementsByClassName("second_name")[i].value)) {
                alert("客户联系人的姓名不能为空");
                return;
            }
            if (isEmpty(document.getElementsByClassName("second_department")[i].value)) {
                alert("客户联系人的部门不能为空");
                return;
            }
            if (isEmpty(document.getElementsByClassName("second_position")[i].value)) {
                alert("客户联系人的职位不能为空");
                return;
            }
            if (isEmpty(document.getElementsByClassName("second_phone")[i].value)) {
                alert("客户联系人的手机号码不能为空");
                return;

            } else if (isNaN(Number(document.getElementsByClassName("second_phone")[i].value))) {
                alert("请输入合法的客户联系人号码");
                return;
            }
            if (isEmpty(document.getElementsByClassName("second_personel")[i].value)) {
                alert("客户联系人的对口人员不能为空");
                return;
            }
            if (isEmpty(document.getElementsByClassName("recent_time")[i].value)) {
                alert("客户联系人的最近沟通时间不能为空");
                return;
            }
        }
        tbRowlength--;
    }


    let name = document.getElementById("custname").value;
    let unit = document.getElementById("unit").value;
    let classify = document.getElementById("classify").value;
    let source = document.getElementById("source").value;
    let serve = document.getElementById("serve").value;
    let servetype = document.getElementById("servetype").value;
    let cus_address = document.getElementById("cus_address").value;
    let level = document.getElementById("level").value;
    let division_code = document.getElementById("division_code").value;
    let zip_code = document.getElementById("zip_code").value;
    let shortname = document.getElementById("short").value;
    let j_person = document.getElementById("j_person").value;
    let organization = document.getElementById("organization").value;
    let web = document.getElementById("web").value;
    let status = document.getElementById("status").value;
    let phone = document.getElementById("phone").value;

    let address = document.getElementById("address").value;
    let mainForm = {
        id: null,
        name: name,
        unit: unit,
        classify: classify,
        source: source,
        serve: serve,
        servetype: servetype,
        cus_address: cus_address,
        address: address,
        level: level,
        division_code: division_code,
        zip_code: zip_code,
        shortname: shortname,
        j_person: j_person,
        organization: organization,
        web: web,
        status: status,
        phone: phone,
    }


    let form = [];
    let rowlength = tb.rows.length;
    let i, j;
    for (i = 1; i < rowlength; i++) {
        let name = tb.rows[i].cells[3].children[0].value;
        let department = tb.rows[i].cells[4].children[0].value;
        let posts = tb.rows[i].cells[5].children[0].value;
        let phone = tb.rows[i].cells[6].children[0].value;
        let personnel = tb.rows[i].cells[7].children[0].value;
        let recenttime = tb.rows[i].cells[8].children[0].value;
        let wechat = tb.rows[i].cells[9].children[0].value;
        let qq = tb.rows[i].cells[10].children[0].value;

        let cjson = {
            id: null,
            name: name,
            department: department,
            posts: posts,
            phone: phone,
            personnel: personnel,
            recenttime: recenttime,
            wechat: wechat,
            qq: qq,
            dept_id: null
        }
        form.push(cjson);
    }


    let url = "http://localhost:8080/CustomersForm/FormUpdate"

    $.ajax({
        type: "post",
        url: url,
        traditional: true,
        data: {
            Customer: JSON.stringify(form),
            Mainform: JSON.stringify(mainForm)
        },
        success: function (data) {
            //传入成功时的操作
            alert("表单提交成功");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //传入失败时的操作
            alert("表单提交失败");
        }
    })


    // console.log("传入servlet前");
    // document.getElementById("mainFormUp").submit();
    // console.log("提交完成后");

}


function addLine() {
    var tb = document.getElementById("firsttable");

    var tr = document.createElement("tr");

    tb.appendChild(tr);

    tr.innerHTML = " <td><input type=\"checkbox\" class=\"selected\" id=\"selected\"></td>\n" +
        "                        <td><span id=\"number\">#</span></td>\n" +
        "                        <td><input class=\"changeButton\" id=\"change\" type=\"button\" value=\"编辑\"></td>\n" +
        "                        <td><input class=\"showText second_name\" type=\"text\" id=\"second_name\" placeholder=\"请输入姓名\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText second_department\" type=\"text\" id=\"second_department\" placeholder=\"请输入部门\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText second_position\" type=\"text\" id=\"second_position\" placeholder=\"请输入职位\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText second_phone\" type=\"text\" id=\"second_phone\" placeholder=\"请输入手机号码\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText second_personel\" type=\"text\" id=\"second_personel\" placeholder=\"请先选择对口人员\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText recent_time\" type=\"date\" id=\"recent_time\" placeholder=\"请输入最近沟通时间\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText\" type=\"text\" id=\"wechat\" placeholder=\"请输入微信号\" value=\"\"></td>\n" +
        "                        <td><input class=\"showText\" type=\"text\" id=\"qq\" placeholder=\"请输入QQ号\" value=\"\"></td>\n" +
        "                        <td><input class=\"deleteButton\" type=\"button\" id=\"deletebutton\" value=\"删除\" id=\"deletebutton\" onclick=\"deleteLine(this)\"></td>"


}

function deleteLine(btn) {
    let tb = document.getElementById("firsttable");
    let tr = btn.parentNode.parentNode;
    tb.deleteRow(tr.rowIndex);
}

function isEmpty(str) {
    if (str == null || str.trim(str) == "") {
        return true;
    }
    return false;

}